﻿using Core.Entities;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Text;

namespace Infrastructure
{
    public class SqlServerContext: DbContext
    {
        public SqlServerContext(DbContextOptions<SqlServerContext> options) : base(options)
        { }

        public DbSet<Department> Department { get; set; }
        public DbSet<Menu> Menu { get; set; }
        public DbSet<Role> Role { get; set; }
        public DbSet<User> User { get; set; }

        protected override void OnModelCreating(ModelBuilder builder)
        {
            //UserRole关联配置
            builder.Entity<UserRole>()
                .Ignore(t => t.User)
                .Ignore(t => t.Role)
                .HasKey(ur => new { ur.UserID, ur.RoleID });

            //RoleMenu关联配置
            builder.Entity<RoleMenu>()
                .Ignore(t => t.Menu)
                .Ignore(t => t.Role)
                .HasKey(rm => new { rm.RoleID, rm.MenuID });

            builder.Entity<Department>()
                .Ignore(t => t.Manager)
                .Ignore(t => t.CreateUser)
                .HasKey("ID");

            builder.Entity<User>()
                .Ignore(t => t.CreateUser)
                .Ignore(t => t.Department)
                .HasKey("ID");

            builder.Entity<Menu>()
                .Ignore(t => t.CreateUser)
                .HasKey("ID");

            builder.Entity<Role>()
                .Ignore(t => t.CreateUser)
                .HasKey("ID");

            base.OnModelCreating(builder);
        }
    }
}
